package com.bawan.vims.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;

/**
 * <p> Title        : DateUtil.java </p>
 * <p> Description	: DateUtil.java </p>
 * <p> Copyright	: Copyright (c) 2015 by FITT</p>
 * <p> Author 		: JOCHEN ZHENG </p>
 * <p> Create		: 2015-1-5 11:31:52 </p> 
 * <p> version 		: 1.0 </p>
 */
public class DateUtil {
	private static char DAY_DELIMITER = '-';
	public static int YEAR = Calendar.YEAR;
	public static int MONTH = Calendar.MONDAY;
	public static int DAY = Calendar.DAY_OF_MONTH;
	public static int HOUR = Calendar.HOUR_OF_DAY;
	public static int MINUTE = Calendar.MINUTE;
	public static int SECOND = Calendar.SECOND;

	public static String MONTH_FORMAT = "yyyy-MM";
	public static String DATE_FORMAT = "yyyy-MM-dd";
	public static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static String DATETIME_LONG_FORMAT = "yyyyMMddHHmmss";
	public static String DATE_LONG_FORMAT = "yyyyMMdd";

	public final static SimpleDateFormat SDF = new SimpleDateFormat(DATE_FORMAT);
	public final static SimpleDateFormat SDF2 = new SimpleDateFormat(DATE_LONG_FORMAT);
	public static final SimpleDateFormat SDFT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 取得当前日期
	 * 
	 * @return YYYY-MM-DD
	 */
	public static String getCurrentDate() {
		return getDateTime().substring(0, 10);
	}

	/**
	 * 取得当前时间
	 * 
	 * @return HH:MM:SS
	 */
	public static String getCurrentTime() {
		return getDateTime().substring(11, 19);
	}

	/**
	 * 获取时间戳，格式为YYYYMMDDhhmmssmmm，如2009-01-22 17:23:45取值为20090122172345000
	 * 
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getTimeStampStr() {
		Calendar calendar = new GregorianCalendar();
		StringBuilder buf = new StringBuilder("");
		buf.append(calendar.get(calendar.YEAR));
		buf.append(calendar.get(calendar.MONTH) + 1 > 9 ? calendar.get(calendar.MONTH) + 1 + "" : "0" + (calendar.get(calendar.MONTH) + 1));
		buf.append(calendar.get(calendar.DAY_OF_MONTH) > 9 ? calendar.get(calendar.DAY_OF_MONTH) + "" : "0" + calendar.get(calendar.DAY_OF_MONTH));
		buf.append(calendar.get(calendar.HOUR_OF_DAY) > 9 ? calendar.get(calendar.HOUR_OF_DAY) + "" : "0" + calendar.get(calendar.HOUR_OF_DAY));
		buf.append(calendar.get(calendar.MINUTE) > 9 ? calendar.get(calendar.MINUTE) + "" : "0" + calendar.get(calendar.MINUTE));
		buf.append(calendar.get(calendar.SECOND) > 9 ? calendar.get(calendar.SECOND) + "" : "0" + calendar.get(calendar.SECOND));
		return buf.toString();
	}

	/**
	 * 取得当前的日期时间
	 * 
	 * @return YYYY-MM-DD HH:MM:DD
	 */
	public static String getDateTime() {
		return getDateTime(new GregorianCalendar());
	}

	/**
	 * 根据日历返回日期时间
	 * 
	 * @param calendar
	 *            日历
	 * @return YYYY-MM-DD HH:MM:DD
	 */
	@SuppressWarnings("static-access")
	private static String getDateTime(Calendar calendar) {
		StringBuffer buf = new StringBuffer("");

		buf.append(calendar.get(calendar.YEAR));
		buf.append(DAY_DELIMITER);
		buf.append(calendar.get(calendar.MONTH) + 1 > 9 ? calendar.get(calendar.MONTH) + 1 + "" : "0" + (calendar.get(calendar.MONTH) + 1));
		buf.append(DAY_DELIMITER);
		buf.append(calendar.get(calendar.DAY_OF_MONTH) > 9 ? calendar.get(calendar.DAY_OF_MONTH) + "" : "0" + calendar.get(calendar.DAY_OF_MONTH));
		buf.append(" ");
		buf.append(calendar.get(calendar.HOUR_OF_DAY) > 9 ? calendar.get(calendar.HOUR_OF_DAY) + "" : "0" + calendar.get(calendar.HOUR_OF_DAY));
		buf.append(":");
		buf.append(calendar.get(calendar.MINUTE) > 9 ? calendar.get(calendar.MINUTE) + "" : "0" + calendar.get(calendar.MINUTE));
		buf.append(":");
		buf.append(calendar.get(calendar.SECOND) > 9 ? calendar.get(calendar.SECOND) + "" : "0" + calendar.get(calendar.SECOND));
		return buf.toString();
	}

	/**
	 * 在指定的日期中按某个时间类型添加指定步长
	 * 
	 * @param datetime
	 *            YYYY-MM-DD HH:MM:SS
	 * @param type
	 *            YEAR,MONTH,DAY,HOUR,MINUTE,SECOND
	 * @param step
	 *            步长 可以是整数或负数
	 * @return 改变后的日期时间 YYYY-MM-DD HH:MM:SS
	 */
	public static String getPreDateTime(String datetime, int type, int step) {

		Calendar calendar = new GregorianCalendar(Integer.parseInt(datetime.substring(0, 4)), Integer.parseInt(datetime.substring(5, 7)) - 1,
				Integer.parseInt(datetime.substring(8, 10)), Integer.parseInt(datetime.substring(11, 13)), Integer.parseInt(datetime.substring(14, 16)),
				Integer.parseInt(datetime.substring(17, 19)));
		calendar.add(type, step);
		return getDateTime(calendar);
	}

	/**
	 * 根据字符串类型取得日期类型
	 * 
	 * @param datetime
	 *            String YYYY-MM-DD HH:MM:SS
	 * @return Calendar
	 */
	public static Calendar getCalendar(String datetime) {
		return new GregorianCalendar(Integer.parseInt(datetime.substring(0, 4)), Integer.parseInt(datetime.substring(5, 7)) - 1, Integer.parseInt(datetime
				.substring(8, 10)), Integer.parseInt(datetime.substring(11, 13)), Integer.parseInt(datetime.substring(14, 16)), Integer.parseInt(datetime
				.substring(17, 19)));
	}

	/**
	 * 在指定的日期中按某个时间类型添加指定步长
	 * 
	 * @param datetime
	 *            YYYY-MM-DD
	 * @param type
	 *            YEAR,MONTH,DAY
	 * @param step
	 *            步长 可以是整数或负数
	 * @return 改变后的日期时间 YYYY-MM-DD
	 */
	public static String getPreDate(String date, int type, int step) {
		Calendar calendar = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1, Integer.parseInt(date
				.substring(8, 10)), 0, 0, 0);
		calendar.add(type, step);
		return getDateTime(calendar).substring(0, 10);
	}

	/**
	 * 给指定日期按某个时间类型添加指定步长
	 * 
	 * @param date
	 *            日期类型
	 * @param type
	 *            YEAR,MONTH,DAY...
	 * @param step
	 *            步长 可以是整数或负数
	 * @return
	 */
	public static Date getDateAddInterval(Date date, int type, int step) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, step);
		return calendar.getTime();
	}

	/**
	 * 取得当前日期的正数
	 * 
	 * @return YYYYMMDD
	 */

	public static int getCurrentDateInt() {
		String date = getCurrentDate();
		return Integer.parseInt(date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10));
	}

	/**
	 * 取得指定日期的正数
	 * 
	 * @return YYYYMMDD
	 */

	public static int getDateInt(String date) {
		return Integer.parseInt(date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10));
	}

	/**
	 * 取得当前日期的正数
	 * 
	 * @return YYYYMMDD
	 */

	public static int getCurrentDateTimeInt() {
		String date = getCurrentDate();
		return Integer.parseInt(date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10));
	}

	/**
	 * 取得指定时间的时间戳
	 * 
	 * @return long 时间戳
	 */
	public static long getTimeStamp(String datetime) {
		return getCalendar(datetime).getTime().getTime();
	}

	/**
	 * 取得当前的时间戳
	 * 
	 * @return long 时间戳
	 */
	public static long getCurrentTimeStamp() {
		return System.currentTimeMillis();
	}

	/**
	 * 取得当前时间的正数
	 * 
	 * @return HHMMSS
	 */

	public static int getCurrentTimeInt() {
		String date = getDateTime();
		return Integer.parseInt(date.substring(11, 13) + date.substring(14, 16) + date.substring(17, 19));
	}

	/**
	 * 结束时间和开始时间之间的时间差
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param type
	 *            1000得到秒，60000得到分，3600000得到小时
	 * @return
	 */
	public static long subtractTime(String endTime, String startTime, long type) {
		SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间
		long result = 0;
		try {
			// 这个的除以1000得到秒，相应的60000得到分，3600000得到小时
			result = (d.parse(endTime).getTime() - d.parse(startTime).getTime()) / type;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获得当前时间的指定格式字符串
	 * 
	 * @return
	 */
	public static String getCurrentDateString(String format) {
		return getDateString(new Date(), format);
	}

	/**
	 * 获得指定时间的指定格式字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateString(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 获取指定日期的Date对象
	 * 
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static Date getStringDate(String dateString, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date date = null;
		try {
			if (!StringUtils.isEmpty(dateString)) {
				date = formatter.parse(dateString);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 格式化时间 输入 yyyymmdd 输出 yyyy-mm-dd
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(String time) {
		return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8);
	}

	/**
	 * example
	 * 
	 * @param args
	 */

	public static void main(String[] args) {
		// String str = DateUtil.getDateTime();
		// System.out.println(str);
		// System.out.println(DateUtil.getDate());
		// System.out.println(DateUtil.getTime());
		// System.out.println(DateUtil.getDateInt());
		// System.out.println(DateUtil.getTimeInt());
		// System.out.println(DateUtil.getPreDateTime(str, HOUR, 100));
		// System.out.println(DateUtil.getPreDateTime(str, SECOND, 100));
		// System.out.println(DateUtil.getPreDateTime(str, MINUTE, 100));
		// System.out.println(DateUtil.getPreDateTime(str, YEAR, 1));
		// System.out.println(DateUtil.getPreDateTime(str, MONTH, 1));
		// System.out.println(DateUtil.getPreDateTime(str, DAY, 10));
		// System.out.println(DateUtil.getTimeStampStr());
		// System.out.println("1" + "\r\n" + "2");
		// String a =
		// "SETUP\r\nCALL_TYPE:1\r\nCALLING_PARTY_NUMBER: 01088822279\r\n0";

//		System.out.println("subtract:" + subtractTime("2010-11-11 12:10:10", "2010-11-11 10:10:00", 3600000));
//
//		Date date = getStringDate("2012-07-27 10:18:32.3", "yyyy-MM-dd hh:mm:ss");
//		String dateString = getDateString(date, "yyyy-MM-dd hh:mm:ss.S");
//		System.out.println(formatTime("20140606"));
		// int port=9080;
		// try {
		//
		// HttpServerProvider httpServerProvider =
		// HttpServerProvider.provider();
		// InetSocketAddress addr = new InetSocketAddress(port <= 0 ?
		// 9080 : port);
		// ServerLogger.getLogger().console("启动HTTP监听端口："+(port <= 0 ?
		// 9080 : port));
		// HttpServerImpl httpServer = (HttpServerImpl) httpServerProvider.
		// createHttpServer(addr, 500);
		// ThreadPoolExecutor threads = new
		// Executors().newThreadPool("system_http_threadpool_"+port);
		// httpServer.setExecutor(threads);
		// httpServer.start();
		// //httpServer.
		// } catch (IOException ex) {
		// ServerLogger.getLogger().error("启动HTTP监听端口["+(port <=0 ?
		// 9080 : port)+"]失败，原因:"+ex.getMessage());
		// ServerLogger.getLogger().error(ex,ex);
		//
		// }
	}
}
